package shop.controller.front.catalog;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import shop.core.FrontBaseController;
import shop.core.Services;
import shop.core.common.bean.JSONResult;
import shop.services.front.systemSetting.bean.SystemSetting;
import shop.services.front.cart.CartService;
import shop.services.front.cart.bean.Cart;
import shop.services.front.catalog.CatalogService;
import shop.services.front.catalog.bean.Catalog;
import shop.services.front.product.ProductService;
import shop.services.front.product.bean.Product;
import shop.services.front.spec.SpecService;
import shop.services.front.spec.bean.Spec;
import shop.services.front.systemSetting.SystemSettingService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.text.DecimalFormat;
import java.util.DoubleSummaryStatistics;
import java.util.List;

;

/**
 * Created by Administrator on 2017/4/17.
 */
@Controller
@RequestMapping("/front/catalog")
public class CatalogController extends FrontBaseController<Catalog> {
    @Resource
    private CatalogService catalogService;
    @Resource
    private ProductService productService;
    @Resource
    private SpecService specService;
    @Resource
    private CartService cartService;
    @Resource
    private SystemSettingService systemSettingService;
    @Override
    public Services<Catalog> getService() {
        return catalogService;
    }

    private static final Logger LOGGER= LoggerFactory.getLogger(CatalogController.class);


    /**
     * 页面跳转方法
     * @return
     */
    @RequestMapping("/toCatalogList")
    public String toCatalogList() {
        return "/front/catalog/catalogList";
    }

    @RequestMapping("/toProductDetail")
    public String toProductList() {
        return "/front/catalog/productDetail";
    }
    @RequestMapping("/toCartList")
    public String toCartList() {
        return "/front/catalog/cartList";
    }
    @RequestMapping("/toMallList")
    public String toMallList(){return "front/catalog/mallList";}

    /**
     * 根据catalogID查询商品列表
     * @param product
     * @return
     */
    @RequestMapping(value="/selectProduct",method= RequestMethod.GET)
    @ResponseBody
    public JSONResult selectProduct(Product product){
        JSONResult jsonResult=new JSONResult();
        DecimalFormat df = new DecimalFormat("######0.00");
        List<Product> list=productService.selectList(product);
        //根据产品列表查询对应的规格、库存
        for(int i=0;i<list.size();i++){
            Spec spec =new Spec();
            spec.setProductID(list.get(i).getId());//获取list的第i个元素
            List<Spec> specs=specService.selectList(spec);
            list.get(i).setSpecs(specs);//把字段specs存入商品
            Double total=specs.get(0).getSpecNowPrice().doubleValue()-specs.get(0).getSpecPurchasePrice().doubleValue();//总利润
            SystemSetting systemSetting=systemSettingService.selectOne(new SystemSetting());//获取系统设定
            Double vProfit=total*(Double.parseDouble(systemSetting.getdPercent()))*(Double.parseDouble(systemSetting.getPercent()));//志愿者可获取佣金
            Double aProfit=total*Double.parseDouble(systemSetting.getPercent())-vProfit;//剩余佣金
            list.get(i).setProfit(df.format(vProfit));
            list.get(i).setAllProfit(df.format(aProfit));
        }
        jsonResult.setData(list);
        jsonResult.setSuccess(true);
        return jsonResult;
    }
    /**
     * 查询商品详情
     * @param product
     * @return
     */
    @RequestMapping(value="/selectProductDetail",method=RequestMethod.GET)
    @ResponseBody
    public JSONResult selectProductDetail(Product product){
        JSONResult jsonResult=new JSONResult();
        DecimalFormat df = new DecimalFormat("######0.00");
        Product rs=productService.selectOne(product);
        //查询产品规格库存列表
        Spec spec =new Spec();
        spec.setProductID(product.getId());
        List<Spec> specs=specService.selectList(spec);
        for(int i=0;i<specs.size();i++){
          specs=specService.selectList(spec);
            Double total=specs.get(0).getSpecNowPrice().doubleValue()-specs.get(0).getSpecPurchasePrice().doubleValue();//总利润
            SystemSetting systemSetting=systemSettingService.selectOne(new SystemSetting());//获取系统设定
            Double vProfit=total*(Double.parseDouble(systemSetting.getdPercent()))*(Double.parseDouble(systemSetting.getPercent()));//志愿者可获取佣金
            Double aProfit=total*Double.parseDouble(systemSetting.getPercent())-vProfit;//剩余佣金
            rs.setProfit(df.format(vProfit));
            rs.setAllProfit(df.format(aProfit));
        }
        rs.setSpecs(specs);
        jsonResult.setData(rs);
        jsonResult.setSuccess(true);
        return jsonResult;
    }

    /**
     * 根据商品ID以及规格id查询商品
     * @param product
     * @return
     */
    @RequestMapping(value = "/selectProductById",method = RequestMethod.GET)
    @ResponseBody
    public JSONResult selectProductById(Product product){
        JSONResult rs=new JSONResult();
        rs.setData(productService.selectProductById(product));
        return rs;
    }
    /**
     * 查询商品规格
     * @param spec
     * @return
     */
    @RequestMapping(value="/selectSpec",method=RequestMethod.GET)
    @ResponseBody
    public JSONResult selectSpec(Spec spec){
        JSONResult jsonResult=new JSONResult();
        List<Spec> list=specService.selectList(spec);
        jsonResult.setData(list);
        jsonResult.setSuccess(true);
        return jsonResult;
    }

    @RequestMapping(value="/selectSpecDetail",method=RequestMethod.GET)
    @ResponseBody
    public JSONResult selectSpecDetail(Spec spec) throws UnsupportedEncodingException {
        JSONResult jsonResult=new JSONResult();
//        String specSpecs=new String(spec.getSpecCombination().getBytes("ISO-8859-1"),"UTF-8");
//        spec.setSpecCombination(specSpecs);
        spec=specService.selectOne(spec);
        jsonResult.setData(spec);
        jsonResult.setSuccess(true);
        return jsonResult;
    }

    /**
     * 添加到购物车方法
     * @param cart
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/insertCart",method=RequestMethod.POST)
    @ResponseBody
    public JSONResult insertCart(Cart cart){
        JSONResult jsonResult=new JSONResult();
        Spec spec=new Spec();
        spec.setProductID(cart.getProductID());
        spec.setSpecCombination(cart.getSpecCombination());
        spec= specService.selectOne(spec);
        cart.setSpecID(spec.getId());
        Cart cart1=cartService.selectOne(cart);
        if(cart1!=null){
            int newCartNum=cart.getCartNum()+cart1.getCartNum();
            cart1.setCartNum(newCartNum);
            cartService.update(cart1);
            jsonResult.setSuccess(true);
            return jsonResult;
        }
        cartService.insert(cart);
        jsonResult.setSuccess(true);
        return jsonResult;
    }
    /**
     * 查询全部购物车方法
     * @param cart
     * @return
     */
    @RequestMapping(value="/selectCart",method=RequestMethod.GET)
    @ResponseBody
    public JSONResult selectCart(Cart cart){
        JSONResult jsonResult=new JSONResult();
        List<Cart> list=cartService.selectList(cart);
        jsonResult.setData(list);
        jsonResult.setSuccess(true);
        return jsonResult;
    }

    /**
     * 查询购物车总数
     * @param cart
     * @return
     */
    @RequestMapping(value = "selectAmount", method = RequestMethod.GET)
    @ResponseBody
    public JSONResult selectAmount(Cart cart) {
        int amount = cartService.selectAmount(cart);
        jsonResult = new JSONResult();
        jsonResult.setData(amount);
        return jsonResult;
    }

    @RequestMapping(value = "/deleteProduct",method = RequestMethod.POST)
    @ResponseBody
    public  JSONResult deleteProduct(Cart cart){
        JSONResult rs=new JSONResult();
        cartService.delete(cart);
        rs.setSuccess(true);
        return rs;
    }
    @RequestMapping(value = "/selectPCatalog",method = RequestMethod.GET)
    @ResponseBody
    public  JSONResult selectPcatalog(){
        JSONResult rs=new JSONResult<>();
        rs.setData(catalogService.selectPcatalog());
        return rs;
    }
    @RequestMapping(value = "/selectChildCatalog",method = RequestMethod.GET)
    @ResponseBody
    public  JSONResult selectPcatalog(Catalog catalog){
        JSONResult rs=new JSONResult<>();
        rs.setData(catalogService.selectChildCatalog(catalog));
        return rs;
    }

    /**
     * 查询推荐商品
     *
     * @return
     * @throws Exception
     */
    @RequestMapping("/selectRecommendProduct")
    @ResponseBody
    public JSONResult selectRecommendProduct(HttpServletRequest request, @ModelAttribute("product") Product product) throws Exception {
        jsonResult = new JSONResult();
        List<Product> rsList = productService.selectRecommendProduct(product);
        for(int i=0;i<rsList.size();i++){
            Spec spec =new Spec();
            spec.setProductID(rsList.get(i).getId());//获取rsList的第i+1个元素
            List<Spec> specList=specService.selectList(spec);
            rsList.get(i).setSpecs(specList);//把字段specs存入商品
        }
        jsonResult.setData(rsList);
        return jsonResult;
    }
}
